//
//  DigitNoteDetailsViewController.m
//  NewApp
//
//  Created by  on 12-7-8.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "DigitNoteDetailsViewController.h"
#import "NAStyledTableViewCell.h"
#import "ProductData.h"
#import "DigitNoteInfoViewController.h"
#import "DigitNoteSearchResultViewController.h"

@interface DigitNoteDetailsViewController ()

@end

@implementation DigitNoteDetailsViewController
@synthesize category;

#pragma mark -
#pragma mark - Views lifecycle and memory management

- (id) initWithCategory:(NACategory *)cat {
    if (self = [super init]) {
        self.category = cat;
    }
    
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //Send request
    [self loadRequest];
    
    [self.view setBackgroundColor:[UIColor clearColor]];
    [self.tableView setBackgroundColor:[UIColor clearColor]];
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    self.tableView.frame = CGRectMake(0.f, 25.f, 320.f, 344.f);
    [self.view addSubview:self.tableView];
    
    _searchBar = [[NASearchBar alloc] initWithFrame:CGRectMake(0.f, 0.f, 320.f, 44.f)];
    _searchBar.delegate = self;
    [self.tableView setTableHeaderView:_searchBar];
}

- (void) dealloc {
    [category release];
    [_searchBar release];
    [super dealloc];
}

#pragma mark -
#pragma mark - Override methods

- (void) requestDidFinished:(RequestEngine *)reqEngine withObject:(id)object {
    if ([object isKindOfClass:[NSDictionary class]]) {
        NSDictionary *result = (NSDictionary *) object;
        [result retain];
        
        self.titleLabel.text = [result objectForKey:@"top_title"];
        self.currentStyle = ([result objectForKey:kStyle] == nil) ? @"D" : [result objectForKey:kStyle];
        
        if ([result objectForKey:kData] != nil) {
            NSArray *array = [result objectForKey:kData];
            for (NSDictionary *dict in array) {
                ShopCarEntity *entity = [[ShopCarEntity alloc] initWithDictionary:dict];
                [self.datasourceObjects addObject:entity];
                [entity release];
            }
        }
        
        [self.tableView reloadData];
        
        [result release];
    }

}

#pragma mark -
#pragma mark - UITableView delegate

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    ShopCarEntity *entity = [self.datasourceObjects objectAtIndex:indexPath.row];
    if (entity != nil && entity.ID != nil) {
//        DigitNoteInfoViewController *viewCtrl = [[DigitNoteInfoViewController alloc] init];
//        viewCtrl.ID = entity.ID;
//        [self.navigationController pushViewController:viewCtrl animated:YES];
//        [viewCtrl release];
        [self viewDetailsWithID:entity.ID];
    }
    
}

#pragma mark -
#pragma mark - UITableView datasource

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.datasourceObjects.count;
}

- (UITableViewCell *) tableView:(UITableView *)tableView 
          cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellIdentifier = @"cellidentifier";
    NAStyledTableViewCell *cell = (NAStyledTableViewCell *)[self.tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[NAStyledTableViewCell alloc] initWithNAStyle:self.currentStyle reuseIdentifier:cellIdentifier];
    }
    
    ShopCarEntity *entity = [self.datasourceObjects objectAtIndex:indexPath.row];
    cell.sID = entity.ID;
    cell.name = entity.item_name;
    cell.createAt = entity.item_no;
    cell.info = entity.info;
    cell.imageURL = [entity.img absoluteString];
    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    cell.showsAccessory = ([self.currentStyle isEqualToString:kStyleD]) ? YES : NO;
    
    cell.backgroundImage = (indexPath.row %2 == 0) ? [UIImage imageNamed:@"news_list_B_item_bg_1.png"] : [UIImage imageNamed:@"news_list_B_item_bg_2.png"];    
    [cell update];
    
    return cell;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 90.f;
}

#pragma mark -
#pragma mark - Publics

- (void) loadRequest {
    [self.engine digitNoteDetailsWithCategoryID:category.sID keyword:@"" page:1];
}

- (void) viewDetailsWithID:(NSString *) aID {
    self.title = @"返回";
    DigitNoteInfoViewController *viewCtrl = [[DigitNoteInfoViewController alloc] init];
    viewCtrl.ID = aID;
    [self.navigationController pushViewController:viewCtrl animated:YES];
    [viewCtrl release];
}

#pragma mark -
#pragma mark - NASearchBarDelegate methods

- (void) searchButtonDidPressed {
    if (_searchBar.keywordTextfield.text.length == 0) 
        return;
        self.title = @"返回";
    DigitNoteSearchResultViewController *viewCtrl = [[DigitNoteSearchResultViewController alloc] initWithID:self.category.sID keyword:_searchBar.keywordTextfield.text];
    [self.navigationController pushViewController:viewCtrl animated:YES];
    [viewCtrl release];
    
}





@end
